var app = sm("do_App");
var page = sm("do_Page");
var openHelper = require("helper/open");
var circleUrl = require("url/circle");
var httpSilence = require("helper/httpSilence");
var userUtil = require("helper/user");
var indexCacher = require("helper/indexCacher");
//头部
ui("headSearch").on("touch",function(){ //搜索
	openHelper.open("source://view/search/search2.ui");
});
ui("headBtn").on("touch",function(){ //创建圈子
	openHelper.open("source://view/circle/base/manage/circleAdd.ui");
});

var aLayoutRoot = ui("aLayoutRoot");
var listview = ui("listView");
var listdata = mm("do_ListData");
listview.bindItems(listdata);
var indexPage = {
	type: 0,
	page: 1,
	totalPages:1,
	cityId: 0,
	tagId: 0,
	tagName: "",
	member: "desc",	//升序asc或降序desc
	init:function(){
		indexPage.getCircle(); //我的圈子
		var _tagCacher = indexCacher.selectTagName();
		if(_tagCacher){
			indexPage.tagId = _tagCacher.id;
			indexPage.tagName = _tagCacher.name;
		}
		
		indexPage.cityId = indexCacher.selectCityId();
	},
	getCircle:function(){
		var userId = userUtil.userId();
		var _url = userId?circleUrl.url.v1_4.myCircle:circleUrl.url.v1_4.newCircle;
		httpSilence.post(_url,{},function(data){
			listdata.removeAll();
			var _data = [],_title="最新圈子";
			if(userId){
				_data = data.circleList;
				_title = "我的圈子";
			}
			else{
				_data = data.page.content;
				_title = "最新圈子";
			}
			listdata.addData([
			   {"template":1,title:_title,"circleBaseList":_data},
			   {"template":2}
          	]);
			indexPage.getList(0,1);
		});
	},
	getList:function(type,page,flag){
		if(flag && page >= indexPage.totalPages){
			indexPage.page = page;
			return;
		}
		var url = "";
		var param = {page:page};
		if(type==0){
			url = circleUrl.url.v1_4.listDefault;
		}
		else if(type==1){//地区
			url = circleUrl.url.v1_4.listZone;
			param.cityId = indexPage.cityId;
		}
		else if(type==2){//话题
			url = circleUrl.url.v1_4.listTagName;
			if(indexPage.tagId==0){
				param.tagName = "";
			}
			else{
				param.tagName = indexPage.tagName;
			}
		}
		else if(type==3){//成员
			url = circleUrl.url.v1_4.listMember;
			param.member = indexPage.member;
		}
		httpSilence.post(url,param,function(data){
			//deviceone.print(url+'--'+JSON.stringify(data));
			var count = listdata.getCount();
			indexPage.totalPages = data.page.totalPages;
			if(count>2&&page==1){
				listdata.removeRange(2,count);
			}
			listdata.addData(data.page.content);
			if(data.page.content.length==0&&page==1){
				listdata.addData([{"template":3}]);
			}
			listview.refreshItems();
		});	
		listview.refreshItems();
	}
	
};
indexPage.init();
page.on("index_dynamic_tab_touch", function(data) {
	indexPage.init();
});


//下拉列表，刷新数据
listview.on("pull", function(data) {
	if (data.state == 2){
		indexPage.init();
		page.fire("setTabIndex",{index:0});
		listview.rebound();
	}
});
//上拉列表，翻页数据
listview.on("push", function(data) {
	if (data.state == 2){
		indexPage.getList(indexPage.type,indexPage.page++,true);
		listview.rebound();
	}
});
//listview滚动，产生工具条总是留在上面的效果。实际上是动态产生一个相同的工具条
var m_layDynTool = aLayoutRoot.add("dynToolbar", "source://view/index/tab/tab.ui", 0, 75);
ui(m_layDynTool).visible=false;
listview.on("scroll", function(data){
	if (data.firstVisiblePosition >= 1) {
		ui(m_layDynTool).visible = true;
	}
	else {
		//图片露出，则删除掉该工具条
		if (ui(m_layDynTool).visible) {
			ui(m_layDynTool).visible=false;
		}
	}
});
page.on("getTabList",function(data){
	indexPage.type = data.index;
	indexPage.page = 1;
	indexPage.member = data.sort?"asc":"desc";
	indexPage.getList(data.index,1);
	page.fire("setTabIndex",data);
});
page.on("getRegionList", function(data){
    if (data.cityId) {
    	indexPage.type = 1;
    	indexPage.page = 1;
    	indexCacher.selectCityId(data.cityId);
		indexCacher.selectCityName(data.cityName);
    	indexPage.cityId = data.cityId;
    	indexPage.cityName = data.cityName;
    	indexPage.getList(1,1);
    }
});
page.on("getHotList", function(data){
    if (data.name) {
    	indexPage.type = 1;
    	indexPage.page = 1;
    	indexPage.tagName = data.name;
    	indexPage.tagId = data.id;
    	var _cacher = {"id":data.id,"name":data.name};
    	indexCacher.selectTagName(JSON.stringify(_cacher));
    	indexPage.getList(2,1);
    }
});
page.on("result", function(data){
    if (data.fromPage && (data.fromPage=="login"||data.fromPage=="loginOut")){
    	indexPage.init();
    }
});